/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.bizon.dinner.gui.topcompoments;

import java.util.logging.Logger;

import org.bizon.dinner.gui.dialogs.SecuritySystem;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(dtd = "-//org.bizon.dinner.gui.topcompoments//Rezults//EN", autostore = false)
public final class RezultsTopComponent extends TopComponent {

	private static RezultsTopComponent instance;
	/** path to the icon used by the component and its open action */
	// static final String ICON_PATH = "SET/PATH/TO/ICON/HERE";
	private static final String PREFERRED_ID = "RezultsTopComponent";

	/**
	 * Obtain the RezultsTopComponent instance. Never call {@link #getDefault}
	 * directly!
	 */
	public static synchronized RezultsTopComponent findInstance() {
		TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID);
		if (win == null) {
			Logger.getLogger(RezultsTopComponent.class.getName()).warning(
					"Cannot find " + PREFERRED_ID + " component. It will not be located properly in the window system.");
			return getDefault();
		}
		if (win instanceof RezultsTopComponent)
			return (RezultsTopComponent) win;
		Logger.getLogger(RezultsTopComponent.class.getName()).warning(
				"There seem to be multiple components with the '" + PREFERRED_ID + "' ID. That is a potential source of errors and unexpected behavior.");
		return getDefault();
	}
            
	/**
	 * Gets default instance. Do not use directly: reserved for *.settings files
	 * only, i.e. deserialization routines; otherwise you could get a
	 * non-deserialized instance. To obtain the singleton instance, use
	 * {@link #findInstance}.
	 */
	public static synchronized RezultsTopComponent getDefault() {
		if (instance == null) {
			instance = new RezultsTopComponent();
		}
		return instance;
	}
    private ResultsTableModel resultsTableModel;

	public RezultsTopComponent() {
		initComponents();
		setName(NbBundle.getMessage(RezultsTopComponent.class, "CTL_RezultsTopComponent"));
		setToolTipText(NbBundle.getMessage(RezultsTopComponent.class, "HINT_RezultsTopComponent"));
		// setIcon(ImageUtilities.loadImage(ICON_PATH, true));

	}

	@Override
	public void componentClosed() {
		// TODO add custom code on component closing
	}

	@Override
	public void componentOpened() {
		// TODO add custom code on component opening
	}

	@Override
	public int getPersistenceType() {
		return TopComponent.PERSISTENCE_NEVER;
	}

	public void open() {
		if (SecuritySystem.isAdmin()) {
			super.open();
		}
	}

	@Override
	protected String preferredID() {
		return PREFERRED_ID;
	}

	private ResultsTableModel getResultsTableModel() {
		if (resultsTableModel == null) {
			resultsTableModel = new ResultsTableModel();
		}
		return resultsTableModel;
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed"
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        jTable1.setModel(getResultsTableModel());
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 501, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 377, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

	private void readPropertiesImpl(java.util.Properties p) {
		// String version = p.getProperty("version");
		// TODO read your settings according to their version
	};

	Object readProperties(java.util.Properties p) {
		if (instance == null) {
			instance = this;
		}
		instance.readPropertiesImpl(p);
		return instance;
	}

	void writeProperties(java.util.Properties p) {
		// better to version settings since initial version as advocated at
		// http://wiki.apidesign.org/wiki/PropertyFiles
		p.setProperty("version", "1.0");
		// TODO store your settings
	}
}
